/**
 * Created by Churcy on 2017/1/9.
 */

window.onload = function() {
	Heartbeat.searchHeartbeat()
	
};

var Heartbeat = {
	searchHeartbeat : function() {
		$("#searchBTN").on('click', function() {
			var conditions = getCurrentConditions();
			conditions.pageNo = 1;
			ajaxSearchHeartbeatList(conditions);
		});
		$("#resetBTN").on('click', function() {
			resetFn();
		});
	}
}
function searchFn() {
	var conditions = getCurrentConditions();
	console.log("searchFN")
	var response = ajaxSearchHeartbeatList(conditions);
}
//重置查询条件
function resetFn() {
	$('#vehNo').val("");
	$('#deviceId').val("");
}
function getCurrentConditions() {
	var vehNo = $('#vehNo').val();
	var deviceId = $('#deviceId').val();

	var pageNo = $("#p").val();
	var APP_KEY = $('#APP_KEY').val();

	var conditions = {
		vehNo : encodeURI(vehNo),
		deviceId : deviceId,
		appKey : APP_KEY,
		pageIndex : pageNo
	};
	return conditions;

}
function ajaxSearchHeartbeatList(conditions) {
	console.log("ajaxSearchHospitalList");
	$.ajax({
		type : "GET",
		url : PROJECT_CTX + "/trafficback/selectHeart",
		data : conditions,
		dataType : "json",
		success : function(data) {
			console.log(data)
			var result = eval(data);
			var resultCode = result.status.code;
			console.log(resultCode)
			console.log(result.line);
			if (1 == resultCode) {
				// 清空当前数据
				var heartbeatList = $("#heartbeatList").empty();
				// 填写查询出来的数据
				$.each(result.line, function(i, item) {
					var tr = "<tr>";
					if (typeof(item.carryCode)=="undefined"){
						item.carryCode = "暂无数据";
					}
					tr += "<td >";
					tr += item.carryCode;
					tr += "</td>"

					tr += "<td >";
					tr += item.deviceId;
					tr += "</td>"

					tr += "<td >";
					tr += item.createTime;
					tr += "</td>"

					tr += "<td >";
					tr += item.locate;
					tr += "</td>"

					/*tr += "<td >";
					tr += " <a title='修改' href='#'>修改</a>";
					tr += " <a title='删除' href='#'>删除</a>";
					tr += "</td >";*/

					tr += "</tr>";
					heartbeatList.append(tr);
					// 更新分页栏
					reloadPage(result);
				});
			}

		}

		,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(XMLHttpRequest.status);
			console.log(XMLHttpRequest.readyState);
			console.log(textStatus);
		}
	});
}

function formatDate(date) {
	var date = new Date(date);
	var month = date.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	var today = date.getDate();
	if (today < 10) {
		today = "0" + today;
	}
	return date.getFullYear() + "-" + month + "-" + today;
}
